/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui.khuyenmai;

import dao.hanghoaDAO;
import java.util.ArrayList;
import java.util.List;
import javax.swing.DefaultListModel;
import pojo.Hanghoa;

/**
 *
 * @author lhaquang
 */
public class ChonHangHoaJDialog extends javax.swing.JDialog {

    private DefaultListModel listModel1;
    private DefaultListModel listModel2;
    private List<Hanghoa> dsKoKM;
    private List<Hanghoa> dsKM;
    
    private boolean isLoaded = false;
    private ChuongTrinhKhuyenMaiJFrame parentFrame;
    /**
     * Creates new form ChonHangHoaJDialog
     */
    public ChonHangHoaJDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.parentFrame = (ChuongTrinhKhuyenMaiJFrame)parent;
        if (!isLoaded) {
            this.listModel1 = new DefaultListModel();
            this.dsKoKM = hanghoaDAO.layDanhSachHangHoaTonTai();
            this.dsKM = new ArrayList<Hanghoa>();
            if (dsKoKM != null && dsKoKM.size() > 0) {
                for (Hanghoa hh : dsKoKM) {
                    this.listModel1.addElement(hh);
                }
            }
            this.listModel2 = new DefaultListModel();
        }
        this.jList1.setModel(listModel1);
        this.jList2.setModel(listModel2);
        this.isLoaded = true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jbtnAdd = new javax.swing.JButton();
        jbtnAddAll = new javax.swing.JButton();
        jbtnRemove = new javax.swing.JButton();
        jbtnRemoveAll = new javax.swing.JButton();
        jbtnThoat = new javax.swing.JButton();
        jbtnOK = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("CHON HANG HOA");

        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        jList2.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane2.setViewportView(jList2);

        jbtnAdd.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jbtnAdd.setText(">");
        jbtnAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAddActionPerformed(evt);
            }
        });

        jbtnAddAll.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jbtnAddAll.setText(">>");
        jbtnAddAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAddAllActionPerformed(evt);
            }
        });

        jbtnRemove.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jbtnRemove.setText("<");
        jbtnRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRemoveActionPerformed(evt);
            }
        });

        jbtnRemoveAll.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jbtnRemoveAll.setText("<<");
        jbtnRemoveAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRemoveAllActionPerformed(evt);
            }
        });

        jbtnThoat.setText("THOAT");
        jbtnThoat.setToolTipText("");
        jbtnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnThoatActionPerformed(evt);
            }
        });

        jbtnOK.setText("OK");
        jbtnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnOKActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel1.setText("Được Khuyến Mãi");
        jLabel1.setToolTipText("");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setText("Không Khuyến Mãi");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(123, 123, 123)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jbtnAdd)
                    .addComponent(jbtnRemoveAll)
                    .addComponent(jbtnAddAll)
                    .addComponent(jbtnRemove))
                .addContainerGap(26, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jbtnOK)
                .addGap(18, 18, 18)
                .addComponent(jbtnThoat)
                .addGap(22, 22, 22))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jbtnAdd, jbtnAddAll, jbtnRemove, jbtnRemoveAll});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(36, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addGap(26, 26, 26)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnAdd)
                        .addGap(5, 5, 5)
                        .addComponent(jbtnAddAll, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnRemove, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnRemoveAll, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnThoat)
                    .addComponent(jbtnOK))
                .addContainerGap())
        );

        jLabel1.getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtnThoatActionPerformed

    private void jbtnAddAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAddAllActionPerformed
        // TODO add your handling code here:
//        Collections.copy(this.dsKoKM, this.dsKM);
        for(int ix = 0;ix<this.dsKoKM.size();ix++){
            this.dsKM.add(this.dsKoKM.get(ix));
        }
        this.dsKoKM.clear();
        this.listModel1.removeAllElements();
        addAllElement(dsKM, listModel2);
    }//GEN-LAST:event_jbtnAddAllActionPerformed

    private void jbtnRemoveAllActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRemoveAllActionPerformed
        // TODO add your handling code here:
        for(int ix = 0;ix<this.dsKM.size();ix++){
            this.dsKoKM.add(this.dsKM.get(ix));
        }
        this.dsKM.clear();
        this.listModel2.removeAllElements();
        addAllElement(dsKoKM, listModel1);
    }//GEN-LAST:event_jbtnRemoveAllActionPerformed

    private void jbtnAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAddActionPerformed
        // TODO add your handling code here:
        Object[] listSelected = this.jList1.getSelectedValues();
        for(Object obj : listSelected){
            if(obj instanceof Hanghoa){
                Hanghoa hh = (Hanghoa)obj;
                this.listModel2.addElement(hh);
                this.listModel1.removeElement(hh);
            }else{
//                log.error("");
            }
        }
    }//GEN-LAST:event_jbtnAddActionPerformed

    private void jbtnRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRemoveActionPerformed
        // TODO add your handling code here:
        Object[] listSelected = this.jList2.getSelectedValues();
        for(Object obj : listSelected){
            if(obj instanceof Hanghoa){
                Hanghoa hh = (Hanghoa)obj;
                this.listModel1.addElement(hh);
                this.listModel2.removeElement(hh);
            }else{
            }
        }
    }//GEN-LAST:event_jbtnRemoveActionPerformed

    private void jbtnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnOKActionPerformed
        // TODO add your handling code here:
        this.parentFrame.setElementForModel(this.dsKM);
        this.setVisible(false);
    }//GEN-LAST:event_jbtnOKActionPerformed

    private void addAllElement(List<Hanghoa> ds, DefaultListModel model){
        if(ds != null && ds.size() > 0){
            for(Hanghoa hh: ds){
                model.addElement(hh);
            }
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jbtnAdd;
    private javax.swing.JButton jbtnAddAll;
    private javax.swing.JButton jbtnOK;
    private javax.swing.JButton jbtnRemove;
    private javax.swing.JButton jbtnRemoveAll;
    private javax.swing.JButton jbtnThoat;
    // End of variables declaration//GEN-END:variables
}
